﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using HZ.MBSM.BLL;

public partial class Bidder_GoodsAdd_CompanySearch_dl : System.Web.UI.Page
{
    public string flag;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(Request.QueryString["flag"]))
        {
            flag = Request.QueryString["flag"];
            if (flag == "1")
            {
                btnAdd.Visible = false;
            }
            showGridView();
        }
        else
        {

        }

    }
    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Company";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "accountid,CompanyName";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", CompanyName = "";
        //UserCode = txtUserCode.Text;
        CompanyName = txtAttorneyName.Text.Trim();
        if (flag == "0")
        {
            strtemp = "Company.Sort='0' and";
        }
        if (flag == "1")
        {
            strtemp = "(Company.Sort='1' or Company.Sort='2') and";
        }
        //if (UserCode != "")
        //{
        //    strtemp = strtemp + " UserCode like '%" + UserCode + "%' and";
        //}
        if (CompanyName != "")
        {
            strtemp = strtemp + " CompanyName like '%" + CompanyName + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;

    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        trAdd.Visible = true;

        tr1.Visible = false;
        DlAttorney.Visible = false;
        DbPageFlip1.Visible = false;
    }
    //保存添加
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtName.Text.Trim() == "")
        {
            Response.Write("<script>alert('请填写“企业名称”');</script>");
        }
        else
        {
            int rowCun = 0;

            Db db1 = new Db();
            System.Data.IDataReader dr;
            string strSql = "select count(AccountId) as cun from Company where CompanyName='" + txtName.Text.Trim() + "' ";
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();


            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该企业在数据库中已经存在！！！');</script>");
                return;
            }
            else
            {


                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.Model.Company _Company = new HZ.MBSM.Model.Company();
                _Company.AccountId = _Account.AccountId;
                _Company.CompanyName = SysFun.ToTrim(txtName.Text);
                if (Company.addgwqiye(_Company))
                {
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "新建", "Company", "Company.addqwqiye(_Company)" + _Company.AccountId, "../Company/GoodsAdd_CompanySearch_dl.aspx");
                    Response.Write("<script>alert('保存成功！！！');</script>");
                }

                trAdd.Visible = false;

                tr1.Visible = true;
                DlAttorney.Visible = true;
                DbPageFlip1.Visible = true;
                showGridView();
            }
        }
    }
    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        trAdd.Visible = false;

        tr1.Visible = true;
        DlAttorney.Visible = true;
        DbPageFlip1.Visible = true;
    }
}
